package com.linchong.concurrency.chapter1;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author:linchong
 * @CreateTime:2019-12-30 21:54
 * @Description:初识并发
 */
@Slf4j
public class ConcurrencyStart {
	public static void main(String[] args) {
		log.info("先读后写为一个逻辑单元，单线程下只能排队处理");
		log.info("预期的结果:先从数据库中取数据，再写入数据到文件");
		readFromDB();
		writeDataToFile();

		new Thread("Read-Thread") {
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					log.info("task 1=>" + i);
				}
			}
		}.start();


		new Thread("Write-Thread") {
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					log.info("task 1=>" + i);
				}
			}
		}.start();
		for (int i = 0; i < 100; i++) {
			log.info("task 2=>" + i);
		}
	}

	private static void readFromDB() {
		try {
			log.info("Begin read data from db.");
			Thread.sleep(1000 * 10L);
			log.info("Read data done and start handle it.");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("the data handle finish and successfully");
	}

	private static void writeDataToFile() {
		try {
			log.info("Begin write data to file.");
			Thread.sleep(2000 * 10L);
			log.info("Write data done and start handle it.");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("the data handle finish and successfully");
	}
}
